<template>
  <div class="login">
      <div class="stu_login">
        <h1 style="font-size:30px;">用户登录</h1>
        <div class="loginForm">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
            <el-form-item label="账号:" prop="user">
              <el-input v-model="ruleForm.user"></el-input>
            </el-form-item>
            <el-form-item label="密码:" prop="pass">
              <el-input type="password" v-model="ruleForm.pass"></el-input>
            </el-form-item>
            <div style="margin-top: 20px">
              <el-button type="primary" class="loginBtn" @click="submitForm('ruleForm')">登录</el-button>
            </div>
            <div style="margin-top: 20px">
              <el-button type="text" class="registerBtn" @click="register">没有账号？去注册</el-button>
            </div>
          </el-form>
        </div>
      </div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        ruleForm: {
          user: '',
          pass: '',
        },
        rules: {
          user: [
            {required: true,message:'请输入邮箱账号',trigger: 'blur' }
          ],
          pass: [
            {required: true,message:'请输入密码',trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$http
                    .post("/api/login", {
                      usr: this.ruleForm.user,
                      pass: this.ruleForm.pass,
                    })
                    .then((res) => {
                      if (res.data.code === 200) {
                        sessionStorage.setItem("token", res.data.token);
                        sessionStorage.setItem("user", res.data.user);
                        this.$message({
                          message: res.data.msg,
                          type: "success",
                        });
                        this.$router.push("/home");
                      } else {
                        this.$message({
                          message: res.data.msg,
                          type: "error",
                        });
                      }
                    });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      register() {
        this.$router.push("/register");
      }
    }
  }
</script>

<style scoped>
.login{
  background-color: #5daf34;
  width: 100%;
  height: 100%;
  background-size: 100% 100%;
  background-position: center center;
  overflow: auto;
  position: relative;
}
.stu_login{
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  height: 330px;
  line-height: 30px;
  margin: -150px 0 0 -190px;
  padding: 40px;
  border-radius: 5px;
  background: #fff;
  border: 1px solid #ccc;
}
.loginForm{
  margin-top: 12px;
}
.loginBtn{
  width: 100%;
}
.registerBtn{
  width: 100%;
}
</style>
